<!doctype html>
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<script>
description("Tests if repeated loading of non-existent img.src resources doesn't deliver onload.");

window.jsTestIsAsync = true;
if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

function finish(final)
{
    // Test handling for missing image resources and
    // ones that load, but with status 404.
    if (!final) {
        testImageOnError(true);
        return;
    }
    window.finishJSTest();
}

function testImageOnError(broken)
{
    var img = new Image();
    img.expectOnLoad = broken;
    img.onload = function() {
        if (img.expectOnLoad)
            testPassed("Received onload for broken img.src");
        else
            testFailed("Received onload for non-existent img.src");

        finish(img.expectOnLoad);
    };
    img.onerror = function () {
        if (img.expectOnLoad) {
            img.onerror = null;
            testFailed("Received onload for boken img.src");
            finish(img.expectOnLoad);
            return;
        }
        else
            testPassed("Received onerror for non-existent img.src");

        if (img.haveFailedAlready) {
            img.onerror = null;
            testPassed("Received error event repeatedly.");
            finish(img.expectOnLoad);
            return;
        }
        img.haveFailedAlready = true;
        img.src = "invalidinvalid2.jpg";
    };
    img.src = broken ? "resources/404image.php" : "resources/non-existent.png";
}
testImageOnError(false);
</script>
<div id="console"></div>
</body>
</html>
